﻿#region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using Csla;
    using Csla.Data;
    using Csla.Reflection;
    using Csla.Server;

    using MarketPulse.BusinessLogic.Common;

    using MarketPulse.DataAccess.DataContextObjects.Production;

#endregion

namespace MarketPulse.DataAccess.Factory
{
    /// <summary>
    /// Abstracts the data access for a VisitStatusTypeList away from a VisitStatusTypeList Business Object.
    /// </summary>
    public class VisitStatusTypeListFactory : ObjectFactory
    {
        #region Member Variables

            /// <summary>
            /// Property that stores the name of the class.
            /// </summary>
            private static readonly Type mClass = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;

            /// <summary>
            /// Property that stores the log4net logger for this class.
            /// </summary>
            private static readonly log4net.ILog mLog = log4net.LogManager.GetLogger(mClass);

        #endregion

        /// <summary>
        /// Method used to fetch a name/value list that represents the available VisitStatusTypes.
        /// </summary>
        /// <returns>The list of name/value pairs representing a VisitStatusType enumeration</returns>
        public VisitStatusTypeList Fetch()
        {
            try
            {
                using (var manager = ContextManager<MUM_dev>.GetManager(Database.BusinessSystem))
                {
                    var visitEventTypeList = (VisitStatusTypeList)MethodCaller.CreateInstance(typeof(VisitStatusTypeList));

                    var resultSet = manager.DataContext.UspVisitStatusTypeSelectAll();

                    // NameValueListBase is a read-only collection,
                    // so typically, when the data access is implemented in
                    // DataPortal_Fetch within the business object, the
                    // private IsReadOnly property must be set false in order to 
                    // populate the collection.  Here, within a factory 
                    // , it isn't visible so one must resort to calling the 
                    // SetIsReadOnly method provided by our parent, ObjectFactory.
                    SetIsReadOnly(visitEventTypeList, false);

                    foreach (UspVisitStatusTypeSelectAllResult result in resultSet)
                    {
                        VisitStatusTypeList.NameValuePair nameValuePair = new VisitStatusTypeList.NameValuePair(result.VisitStatusTypeGUID.Value, result.VisitStatusTypeName);
                        visitEventTypeList.Add(nameValuePair);
                    }

                    SetIsReadOnly(visitEventTypeList, true);

                    return visitEventTypeList;
                }
            }       
            catch (Exception ex)
            {
                mLog.Error(ex.ToString());
                throw ex;
            }
        }
    }
}

